package controlador;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FrontController extends HttpServlet {

    private String vistas[] = {"usuario", "inicio", "flujo", "paso", "evento",
        "solicitud", "factura", "proveedor", "causa",
        "reporte", "responsable", "historico"};
    private String acciones[] = {"listado", "nuevo", "editar", "detalle", "reporte", "bandeja"};
    /*
     * v: 0.-usuario 1.-inicio 2.-flujo 3.-paso 4.-evento 5.-solictud 6.-factura 7.-proveedor 
     * 8.-causa 9.-reporte 10.-responsable, 11.-historico
     * 
     * 
     * ac: 0.-listado 1.-nuevo 2.-editar 3.-detalle 4.-reporte 5.-bandeja
     */

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        if (request.getSession().getAttribute("sUsuario") == null) {
//            out.println("<script type='text/javascript'>");
//            out.println("location.reload();");
//            out.println("</script>");
            //out.println("<meta http-equiv=\"refresh\" content=\"1\">");
            //request.getSession().invalidate();
            response.reset();
            response.sendRedirect(getServletContext().getContextPath());
        }
        try {
            int vista, accion;
            vista = request.getParameter("v") == null ? -1 : Integer.parseInt(request.getParameter("v"));
            accion = request.getParameter("ac") == null ? -1 : Integer.parseInt(request.getParameter("ac"));
            String url = "/jsp/" + vistas[vista] + "/" + acciones[accion] + ".jsp";
            //out.println(url);
            getServletContext().getRequestDispatcher(url).forward(request, response);
        } catch (Exception e) {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Error - Front Controller</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("Error al cargar la pagina solicitada");
            out.println("<br/><br/><br/>" + e.toString() + "<br/>" + e.getMessage());
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
